package com.guli.gulimall.product.service.impl;

import com.guli.common.utils.Query;
import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.vo.AttrgroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @SuppressWarnings("all")
    @Autowired
    CategoryDao categoryDao;

    @SuppressWarnings("all")
    @Autowired
    AttrDao attrDao;
    @Override
    public PageUtils queryPage(Long catelogId,Map<String, Object> params) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        if (catelogId!=null && !(catelogId.equals(0l))){
            wrapper.eq("catelog_id",catelogId);
        }


        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrgroupVo> getcatelogId(Long catelogId) {
        //拿到当前分类的所有分组
        List<AttrGroupEntity> cateloglist = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //拿到该分组下面的所有规格参数
        List<AttrgroupVo> collect = cateloglist.stream().map(item -> {
            AttrgroupVo attrgroupVo = new AttrgroupVo();
            BeanUtils.copyProperties(item, attrgroupVo);
            //通过当前的分组编号拿到底下所有的规格参数
            Long attrGroupId = item.getAttrGroupId();
            List<AttrEntity> attrEntities = attrDao.selectList(new QueryWrapper<AttrEntity>().eq("attr_group_id", attrGroupId)
                    .eq("attr_type", 1));
            //遍历
            List<AttrgroupVo.AttrVo> collect1 = attrEntities.stream().map(arr -> {
                ;
                AttrgroupVo.AttrVo attrVo = new AttrgroupVo.AttrVo();
                BeanUtils.copyProperties(arr, attrVo);
                return attrVo;
            }).collect(Collectors.toList());

            attrgroupVo.setAttrs(collect1);
            return attrgroupVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public boolean save(AttrGroupEntity entity) {
        Long catelogId = entity.getCatelogId();
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        entity.setCatelogName(categoryEntity.getName());
        return super.save(entity);
    }

    @Override
    public AttrGroupEntity getById(Serializable id) {
        //查询当前行属性的值
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(id);
        //拿到分类的三级id
        Long catelogId = attrGroupEntity.getCatelogId();
        //创建一个集合往里面存放值
        ArrayList<Long> list = new ArrayList<>();
        //调用私有方法拿到二级与一级的id并且存放到集合里面
        getcatelodIdparnet(catelogId,list);

        //反转
        Collections.reverse(list);
        attrGroupEntity.setCatelogIds(list.toArray(new Long[list.size()]));
        //转成数组把arrlist存进去
        return attrGroupEntity;
    }
    private void getcatelodIdparnet(Long catelogId,ArrayList<Long> list){
        //先把三级id存进去
        list.add(catelogId);
        //通过类型三级id 查询三级类型对象
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (!categoryEntity.getParentCid().equals(0l)){
            Long parentCid = categoryEntity.getParentCid();
            getcatelodIdparnet(parentCid,list);
        }

    }

    @Override
    public boolean updateById(AttrGroupEntity entity) {
        Long catelogId = entity.getCatelogId();
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        entity.setCatelogName(categoryEntity.getName());
        return super.updateById(entity);
    }
}